package wang_si_bo;
/**
 * @Author WangSibo
 * @Date 2021/1/23 16:37
 * @Version 1.0
 */
public class WANG_SI_BO_23_Q2<T> {
    private Node head; //首节点
    private Node last;//尾节点
    private int S;//元素的个数
    private class Node{
        public T item;
        public Node next;
        public Node(T item, Node next){
            this.item = item;
            this.next = next;
        }
    }
    public WANG_SI_BO_23_Q2(){
        this.head = new Node(null,null); //默认
        this.last = null;
        this.S=0;//默认为0
    }
    //判断队列是否为空
    public boolean isK(){
        return S==0;
    }
    //向队列中插入元素t
    public void insert(T t){
        if (last==null){
            //如果尾节点last为null，则让新结点指向尾节点
            last = new Node(t,null);
            head.next = last;  //首节点指向尾结点
        }else{
            Node oldLast = last; //将新插入的结点设为尾结点
            last = new Node(t,null);
            oldLast.next = last;  //尾结点指向新尾结点
        }
        S++;
    }
    //从队列中拿出一个元素
    public T delect(){
        if (isK()){ //如果是空链表，直接返回空
            return null;
        }
        Node oldFirst = head.next;  //首节点指向旧的结点的下一个结点
        head.next = oldFirst.next;
        S--;

        if (isK()){ //如果队列中的元素没有了，重置last=null
            last=null;
        }
        return oldFirst.item;
    }
}
